﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Word = Microsoft.Office.Interop.Word;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Word;
using Microsoft.Office.Tools.Word.Extensions;
using System.IO;
using System.Diagnostics;
using biolit_config;

namespace UCSD_BioLit
{
    public class ApplicationEvents_Handlers
    {
        /// <summary>
        /// class name for exception title.
        /// </summary>
        private const string ExceptionTitle = "ApplicationEvent_Handlers";

        /// <summary>
        /// Event handler for WindowsBeforeDoubleClick event used to lookup terms for biolit namespace.
        /// </summary>
        /// <param name="sel">selection text range</param>
        /// <param name="cancel">to cancel the event bubble</param>
        public void Application_WindowBeforeDoubleClick(Microsoft.Office.Interop.Word.Selection sel, ref bool cancel)
        {
            cancel = false;
            try
            {                
                if (sel.ContentControls.Count > 0)
                {
                    // used to retrieve the controls under biolit namespace.
                    foreach (Word.ContentControl node in sel.ContentControls)
                    {
                        if (WordCommonFuncs.CheckControlNamespace(node.Tag, ThisAddIn.BIOLIT_NS_URI))
                        {                            
                            DocumentWrapper wordDoc = Globals.ThisAddIn.FindActiveWrapper();
                            wordDoc.LookupTerm(node.Range.Text);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message, ExceptionTitle);
            }
        }

        /// <summary>
        /// Document open event handler method.
        /// </summary>
        /// <param name="doc">document to open</param>
        public void Application_DocumentOpen(Microsoft.Office.Interop.Word.Document doc)
        {
            ThisAddIn ad = Globals.ThisAddIn;
            ad.AddDocument(doc);
            try
            {          
             // Load the control collection.
              this.LoadControlProperties(doc);               
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message, ExceptionTitle);
            }
        }

        /// <summary>
        /// New Document event handler method.
        /// </summary>
        /// <param name="doc">new document object</param>
        public void Application_DocumentNew(Microsoft.Office.Interop.Word.Document doc)
        {
            ThisAddIn ad = Globals.ThisAddIn;
            ad.AddDocument(doc);            
        }       

        /// <summary>
        /// Method used to load the control collection.
        /// </summary>
        /// <param name="doc">active document</param>
        public void LoadControlProperties(Microsoft.Office.Interop.Word.Document doc)
        {
            // Load the persisted state of dynamic controls.
            // prefix the "urn:schemas-microsoft-com." for declaration of the custom namespace.
            ControlProperties[] savedControls = ControlsStorage.Load(doc, ThisAddIn.BIOLIT_NS_URI);
            if (savedControls != null && savedControls.Length > 0 && ControlsStorage.ControlCollection != null)
            {
                // add the controls with thier id into control collection.
                foreach (ControlProperties properties in savedControls)
                {
                    Guid propertiesID = WordCommonFuncs.ConvertToGuid(properties.CustomXMLId);
                    if (propertiesID != null && propertiesID != Guid.Empty)
                    {
                        ControlsStorage.ControlCollection.Add(propertiesID, properties);
                    }
                }
            }
        }
    }
}
